package org.setec.utils;

import org.apache.commons.lang.ObjectUtils;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class Utils {


    public static Class[] objectClasses(Object[] objects) {
        Class[] classes = new Class[objects == null ? 0 : objects.length];
        for (int i = 0; objects != null && i < objects.length; i++) {
            classes[i] = objects[i]!=null?objects[i].getClass():ObjectUtils.Null.class;
        }
        return classes;
    }


    public static String getMD5Hash(byte[] data) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte result[] = md5.digest(data);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < result.length; i++) {
                String s = Integer.toHexString(result[i]);
                int length = s.length();
                if (length >= 2) {
                    sb.append(s.substring(length - 2, length));
                } else {
                    sb.append("0");
                    sb.append(s);
                }
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

    public static String findFile(String name){

        File file = new File("foo");
        String currentDir = file.getAbsolutePath();
        currentDir = currentDir.substring(0, currentDir.lastIndexOf(System.getProperty("file.separator")));
        Set dirs = new HashSet();
        dirs.add(currentDir);
        scanForDirs(dirs, currentDir, 3, 5);
        return scanForFile(dirs, name);
    }

    private static void scanForDirs(Set dirs, String startDir, int depthDown, int depthUp){

        File currentDir = new File(startDir);

        if( depthUp==0 ){
            return;
        } else {
            File[] files = currentDir.listFiles();
            for( Iterator it = Arrays.asList(files).iterator(); it.hasNext(); ) {
                 File file = (File) it.next();
                if( file.isDirectory() ){
                    if( !dirs.contains(file.getAbsolutePath()) ) {
                        dirs.add(file.getAbsolutePath());
                        scanForDirs(dirs, file.getAbsolutePath(), 0, depthUp-1);
                    }
                }
            }
        }

        if( depthDown==0 ){
            return;
        } else {
            File subDir = currentDir.getParentFile();
            if( subDir!=null ){
                scanForDirs(dirs, currentDir.getAbsolutePath(), depthDown-1, depthUp+1);
            }
        }
    }


    private static String scanForFile(Set dirList, String fileName){

        for( Iterator dit = dirList.iterator(); dit.hasNext(); ){
            String dir = (String) dit.next();
            File dirFile = new File(dir);
            File[] files = dirFile.listFiles();
            for( Iterator fit = Arrays.asList(files).iterator(); fit.hasNext(); ){
                File file = (File) fit.next();
                if( file.isFile() ){
                    if( file.getName().equals(fileName) ){
                        return file.getAbsolutePath();
                    }
                }
            }
        }
        return null;
    }

    public static String formatStackTrace(StackTraceElement[] stackTraceElements){
        StringBuffer result = new StringBuffer();
        for( Iterator it = Arrays.asList(stackTraceElements).iterator(); it.hasNext(); ){
            StackTraceElement stackTraceElement = (StackTraceElement) it.next();
            result.append(stackTraceElement.toString());
            result.append("\n");
        }
        return result.toString();
    }

}
